<?php
namespace api\modules\v1\models;

use api\components\ApiModel;
use common\models\ARServiceCharge;
use Yii;

/**
 * 手续费模型
 * @author WJ
 */
class ServiceCharge extends ApiModel
{
    /**
     * 获取手续费安全码
     * @param Object $row 数据记录
     */
    public static function getChargeSecufity($row)
    {
        $scale = Yii::$app->params['scale'];
        $salt = Yii::$app->params['salt']['order'];
        $param = [
            'uid'      => $row->uid,
            'coin'     => $row->coin,
            'order_id' => $row->order_id,
            'amount'   => sprintf("%.{$scale}f", $row->amount),
            'status'   => $row->status,
        ];
        $addtime = $row->security_at;
        $code = self::getSecurity($param, $addtime, $salt);
        return $code;
    }

    /**
     * 校验手续费安全码
     * @param Object $row 数据记录
     */
    public static function verifyChargeSecufity($row)
    {
        $code = self::getChargeSecufity($row);
        if ($code === $row->security_code) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 添加手续费记录
     */
    public static function AddServiceCharge($order)
    {
        $charge = new ARServiceCharge();
        $charge->uid = $order->broker;
        $charge->coin = $order->coin;
        $charge->order_id = $order->id;
        $charge->amount = $order->broker_charge;
        $charge->status = ARServiceCharge::STATUSTRANSFER;
        $charge->security_at = Yii::$app->formatter->asTimeStamp('now');
        $security_code = self::getChargeSecufity($charge);
        $charge->security_code = $security_code;
        if(!$charge->save()){
            self::getErrorMessge($charge);
        }
        return true;
    }
}
